import { App } from 'vue';
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import { Layout } from '@/router/constant';
import { renderIcon } from '@/utils';
import { RedirectRoute } from '@/router/base';
import { PageEnum } from '@/enums/pageEnum';
import { createRouterGuards } from './router-guards';
import { SettingOutlined } from '@vicons/antd';
const modules = import.meta.globEager('./modules/**/*.ts');

const routeModuleList: RouteRecordRaw[] = [];

Object.keys(modules).forEach((key) => {
  const mod = modules[key].default || {};
  const modList = Array.isArray(mod) ? [...mod] : [mod];
  routeModuleList.push(...modList);
});

function sortRoute(a, b) {
  return (a.meta?.sort || 0) - (b.meta?.sort || 0);
}

routeModuleList.sort(sortRoute);

export const RootRoute: RouteRecordRaw = {
  path: '/',
  name: 'Root',
  redirect: PageEnum.BASE_HOME,
  meta: {
    title: 'Root',
  },
};
export const LoginRoute: RouteRecordRaw = {
  path: '/login',
  name: 'Login',
  component: () => import('@/views/login/index.vue'),
  meta: {
    title: '登录',
  },
};
export const RegisterRoute: RouteRecordRaw = {
  path: '/register',
  name: 'register',
  component: () => import('@/views/register/Register.vue'),
  meta: {
    title: '注册',
    hidden: true,
  },
};
export const FindPwdRoute: RouteRecordRaw = {
  path: '/findPwd',
  name: 'findPwd',
  component: () => import('@/views/login/find-pwd.vue'),
  meta: {
    title: '找回密码',
    hidden: true,
  },
};
export const ResetResult: RouteRecordRaw = {
  path: '/reset-result',
  name: 'reset-result',
  meta: {
    title: '重置密码结果',
    hidden: true,
  },
  component: () => import('@/views/login/reset-result.vue'),
};
export const RegisterResult: RouteRecordRaw = {
  path: '/register-result',
  name: 'register-result',
  meta: {
    title: '注册结果',
    hidden: true,
  },
  component: () => import('@/views/register/register-result.vue'),
};
export const RelateRoute: RouteRecordRaw = {
  path: '/relate',
  name: 'relate',
  meta: {
    title: '绑定账号',
    hidden: true,
  },
  component: () => import('@/views/login/relate.vue'),
};
export const SettingRoute: RouteRecordRaw = {
  path: '/setting',
  name: 'Setting',
  redirect: '/setting/account',
  component: Layout,
  meta: {
    title: '设置页面',
    icon: renderIcon(SettingOutlined),
    hidden: true,
    sort: 5,
  },
  children: [
    {
      path: 'account',
      name: 'setting-account',
      meta: {
        title: '个人设置',
        hidden: true,
      },
      component: () => import('@/views/setting/account/account.vue'),
    },
    {
      path: 'system',
      name: 'setting-system',
      meta: {
        title: '系统设置',
        hidden: true,
      },
      component: () => import('@/views/setting/system/system.vue'),
    },
  ],
};

//需要验证权限
export const asyncRoutes = [...routeModuleList];

//普通路由 无需验证权限
export const constantRouter: any[] = [
  SettingRoute,
  LoginRoute,
  RelateRoute,
  RegisterRoute,
  FindPwdRoute,
  RootRoute,
  RedirectRoute,
  ResetResult,
  RegisterResult,
];

const router = createRouter({
  history: createWebHistory(),
  routes: constantRouter,
  strict: true,
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

export function setupRouter(app: App) {
  app.use(router);
  // 创建路由守卫
  createRouterGuards(router);
}

export default router;
